package run.autoium.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import run.autoium.entity.po.ProjectParam;
import run.autoium.entity.vo.ProjectParamVo;
import run.autoium.mapper.ProjectParamMapper;
import run.autoium.service.ProjectParamService;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 参数化 服务实现类
 * </p>
 *
 * @author Ks1r
 * @since 2020-11-28
 */
@Service
public class ProjectParamServiceImpl extends ServiceImpl<ProjectParamMapper, ProjectParam> implements ProjectParamService {

    public ProjectParamVo[] recordQuery(Page<ProjectParam> page, ProjectParamVo queryVo) {
        QueryWrapper<ProjectParam> wrapper = new QueryWrapper<>();

        String type = queryVo.getTypee();
        String name = queryVo.getName();
        String value = queryVo.getValue();
        String environment = queryVo.getEnvironment();
        String description = queryVo.getDescription();
        Date begin = queryVo.getBegin();
        String end = queryVo.getEnd();

        if (!StringUtils.isEmpty(type)) wrapper.eq("typee", type);
        if (!StringUtils.isEmpty(name)) wrapper.eq("name", name);
        if (!StringUtils.isEmpty(value)) wrapper.ge("value", value);
        if (!StringUtils.isEmpty(environment)) wrapper.le("environment", environment);
        if (!StringUtils.isEmpty(description)) wrapper.le("description", description);
        if (!StringUtils.isEmpty(begin)) wrapper.ge("gmt_create", begin);
        if (!StringUtils.isEmpty(end)) wrapper.le("gmt_create", end);

        wrapper.orderByDesc("gmt_create");
        baseMapper.selectPage(page, wrapper);

        // 把po对象转为vo对象
        List<ProjectParam> records = page.getRecords();
        int len = records.size();
        ProjectParamVo[] res = new ProjectParamVo[len];
        for (int i = 0; i < len; ++i) {
            ProjectParamVo vo = new ProjectParamVo();
            ProjectParam record = records.get(i);
            vo.setId(record.getId());
            vo.setTypee(String.valueOf(record.getTypee()));
            vo.setName(record.getName());
            vo.setValue(record.getValue());
            switch (String.valueOf(record.getEnvironment())) {
                case "1":
                    vo.setEnvironment("dev");
                    break;
                case "2":
                    vo.setEnvironment("test");
                    break;
                case "3":
                    vo.setEnvironment("online");
                    break;
                default:
                    vo.setEnvironment("dev");
                    break;
            }
            vo.setDescription(record.getDescription());
            vo.setBegin(record.getGmtCreate());
            res[i] = vo;
        }

        return res;
    }
}
